package com.million.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.commonutils.JwtUtils;
import com.million.commonutils.orderVo.UcenterMemberVo;
import com.million.eduservice.client.UserClient;
import com.million.eduservice.entity.EduComment;
import com.million.eduservice.entity.vo.comment.CommentVo;
import com.million.eduservice.entity.vo.comment.QueryComment;
import com.million.eduservice.mapper.EduCommentMapper;
import com.million.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.million.servicebase.exception.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author 十麓
 * @since 2022-04-07
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    @Autowired
    private UserClient userClient;

    //根据课程id 分页查询课程评论信息
    @Override
    public Map<String, Object> selectCommentPage (Page<EduComment> eduCommentPage, String courseId) {
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        Page<EduComment> pageParams = baseMapper.selectPage(eduCommentPage, queryWrapper);
        List<EduComment> records = pageParams.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("current", pageParams.getCurrent());
        map.put("pages", pageParams.getPages());
        map.put("size", pageParams.getSize());
        map.put("total", pageParams.getTotal());
        map.put("hasNext", pageParams.hasNext());
        map.put("hasPrevious", pageParams.hasPrevious());
        return map;
    }

    //添加课程评论
    @Override
    public String addComment (EduComment eduComment, HttpServletRequest request) {
        //得到会员id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return null;
        }
        //设置会员id
        eduComment.setMemberId(memberId);
        //服务调用service-ucenter
        UcenterMemberVo ucenterMember = userClient.getMemberInfoById(memberId);
        eduComment.setNickname(ucenterMember.getNickname());
        eduComment.setAvatar(ucenterMember.getAvatar());
        int insert = baseMapper.insert(eduComment);
        if (insert == 0) {
            throw new GuliException(20001, "评论失败");
        }
        return memberId;
    }

    //分页条件查询课程评论 渲染后台【评论功能
    @Override
    public Page<CommentVo> selectCommentVoPage (Page<CommentVo> commentVoPage, QueryComment queryComment) {
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmtCreate");
        if (queryComment == null) {
            return baseMapper.selectCommentVoList(commentVoPage, queryWrapper);
        }
        if (!StringUtils.isEmpty(queryComment.getCourseTitle())) {
            queryWrapper.ge("courseTitle", queryComment.getCourseTitle());
        }
        if (!StringUtils.isEmpty(queryComment.getStart())) {
            queryWrapper.ge("gmtCreate", queryComment.getStart());
        }
        if (!StringUtils.isEmpty(queryComment.getEnd())) {
            queryWrapper.le("gmtCreate", queryComment.getEnd());
        }

        return baseMapper.selectCommentVoList(commentVoPage, queryWrapper);
    }
}
